import com.avaje.ebean.Ebean;
import models.core.BaseAttrSpec;
import models.CustomerInformation;
import models.core.CompositeAttrSpec;
import models.production.TaskSpec;
import models.Product;
import models.menu.Menu;
import models.inventory.ResourceSpec;
import play.GlobalSettings;
import play.Logger;
import play.libs.Yaml;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Gipxy group
 * Date: 4/21/12
 */
public class Global extends GlobalSettings {

    
    public void onStart(play.Application app) {
        Logger.info("Application has started!");

        initData();
    }

    public static void initData() {
        if(Menu.find.all().size()==0) {
            Map<String,List<Object>> all = (Map<String,List<Object>>) Yaml.load("data/initial-menu.yml");
            //delete all
            Ebean.delete(Menu.find.all());
            // Insert users first
            Ebean.save(all.get("menus"));

            System.out.println("Init of menus="+Menu.find.all().size());
        }

        if (Product.find.all().size() ==0 ) {
            initProd();
        }

        if (BaseAttrSpec.find.all().size() ==0 ) {
            initTask();
        }
    }

    private static void initProd() {
        Map<String,List<Object>> all = (Map<String,List<Object>>) Yaml.load("data/initial-product.yml");
        // Insert customer first
        Ebean.save(all.get("customers"));
        System.out.println("Just init customers="+ CustomerInformation.find.all().size());

        //
        Ebean.save(all.get("products"));
        Ebean.save(all.get("orders"));

        Ebean.save(all.get("prodRequests"));

        System.out.println("Just init prods="+Product.find.all().size());
    }

    private static void initTask() {
        Map<String,List<Object>> all = (Map<String,List<Object>>) Yaml.load("data/initial-tasks.yml");
        // Insert customer first
        List<Object> resourceSpecs = (List<Object>)all.get("resourceSpecs");
        List<Object> taskSpecs = (List<Object>)all.get("taskSpecs");
        /*TaskSpec taskSpec = (TaskSpec) taskSpecs.get(0);
        ResourceSpec resourceSpec = (ResourceSpec) resourceSpecs.get(0);
        taskSpec.resourceSpecs.add(resourceSpec);
        System.out.println("task id:"+taskSpec.id);*/
        Ebean.save(all.get("attrSpecs"));
        Ebean.save(all.get("compositeAttrSpecs"));
        //Ebean.save(all.get("baseAttrValues"));

        for (Object one: resourceSpecs){
            List<BaseAttrSpec> lst = new ArrayList<BaseAttrSpec>();
            ResourceSpec resourceSpec = (ResourceSpec) one;
            for(BaseAttrSpec attrSpec: resourceSpec.baseAttrSpecs){
                BaseAttrSpec db = BaseAttrSpec.find.byId(attrSpec.id);
                if(db!=null){
                    lst.add(db);
                    System.out.println("here");
                }
            }
            resourceSpec.baseAttrSpecs.clear();
            resourceSpec.baseAttrSpecs.addAll(lst);
        }

        Ebean.save(resourceSpecs);
        for (Object one: taskSpecs){
            List<ResourceSpec> lst = new ArrayList<ResourceSpec>();
            TaskSpec taskSpec = (TaskSpec) one;
            for(ResourceSpec resourceSpec: taskSpec.resourceSpecs){
                lst.add(ResourceSpec.find.byId(resourceSpec.id));
            }
            taskSpec.resourceSpecs.clear();
            taskSpec.resourceSpecs.addAll(lst);
        }
        Ebean.save(taskSpecs);

        System.out.println("Just init BaseAttrSpec=" + BaseAttrSpec.find.all().size());
        System.out.println("Just init taskSpecs=" + TaskSpec.find.all().size());
        System.out.println("Just init CompositeAttrSpec=" + CompositeAttrSpec.find.all().size());
        System.out.println("Just init resourceSpecs=" + ResourceSpec.find.all().size());
        System.out.println("resource spec in task 1: " + TaskSpec.find.byId(1l).resourceSpecs.size());
        System.out.println("attribute spec in resource 2: " + ResourceSpec.find.byId(2l).baseAttrSpecs.size());
    }


    public void onStop(play.Application app) {
        Logger.info("Application shutdown...");
    }


    

}
